home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 October / CHIP Turkiye Ekim 2000.iso / prog / naps / 04 / setup.exe / Gnucleus / PrefUpload.cpp < prev    next >
C/C++ Source or Header  |  2000-07-15  |  9KB  |  383 lines

  1. /********************************************************************************
  2.  
  3.     Gnucleus - A node application for the Gnutella network
  4.     Copyright (C) 2000 John Marshall
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     For support, questions, comments, etc...
  20.     E-Mail: 
  21.         swabby@c0re.net
  22.     
  23.     Address:
  24.         21 Cadogan Way
  25.         Nashua, NH, USA 03062
  26.  
  27. ********************************************************************************/
  28.  
  29. // PrefUpload.cpp : implementation file
  30. //
  31.  
  32. #include "stdafx.h"
  33. #include "Gnucleus.h"
  34. #include "GnucleusDoc.h"
  35. #include "PrefUpload.h"
  36.  
  37. #ifdef _DEBUG
  38. #define new DEBUG_NEW
  39. #undef THIS_FILE
  40. static char THIS_FILE[] = __FILE__;
  41. #endif
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CPrefUpload property page
  45.  
  46. IMPLEMENT_DYNCREATE(CPrefUpload, CPropertyPage)
  47.  
  48. CPrefUpload::CPrefUpload() : CPropertyPage(CPrefUpload::IDD)
  49. {
  50.     Doc = NULL;
  51.  
  52.     //{{AFX_DATA_INIT(CPrefUpload)
  53.     //}}AFX_DATA_INIT
  54. }
  55.  
  56. CPrefUpload::~CPrefUpload()
  57. {
  58. }
  59.  
  60. void CPrefUpload::DoDataExchange(CDataExchange* pDX)
  61. {
  62.     CPropertyPage::DoDataExchange(pDX);
  63.     //{{AFX_DATA_MAP(CPrefUpload)
  64.     DDX_Control(pDX, IDC_EDIT_SHARED, m_ebEditShared);
  65.     DDX_Control(pDX, IDC_CHECK_RECURSE, m_chkRecurse);
  66.     DDX_Control(pDX, IDC_CHECK_CLEAR_UL, m_chkClearUL);
  67.     DDX_Control(pDX, IDC_EDIT_MAX_UL, m_ebMaxUL);
  68.     DDX_Control(pDX, IDC_CHECK_MAX_UL, m_chkMaxUL);
  69.     DDX_Control(pDX, IDC_LIST_SHARED, m_lstShared);
  70.     //}}AFX_DATA_MAP
  71. }
  72.  
  73.  
  74. BEGIN_MESSAGE_MAP(CPrefUpload, CPropertyPage)
  75.     //{{AFX_MSG_MAP(CPrefUpload)
  76.     ON_BN_CLICKED(IDC_CHECK_MAX_UL, OnCheckMaxUl)
  77.     ON_EN_CHANGE(IDC_EDIT_MAX_UL, OnChangeEditMaxUl)
  78.     ON_BN_CLICKED(IDC_CHECK_CLEAR_UL, OnCheckClearUl)
  79.     ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
  80.     ON_EN_CHANGE(IDC_EDIT_SHAREDIR, OnChangeEditSharedir)
  81.     ON_BN_CLICKED(IDC_BUTTON_REMOVE, OnButtonRemove)
  82.     ON_BN_CLICKED(IDC_BUTTON_BROWSE_SHARE, OnButtonBrowseShare)
  83.     ON_NOTIFY(NM_CLICK, IDC_LIST_SHARED, OnClickListShared)
  84.     //}}AFX_MSG_MAP
  85. END_MESSAGE_MAP()
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CPrefUpload message handlers
  89.  
  90.  
  91. BOOL CPrefUpload::OnInitDialog() 
  92. {
  93.     CPropertyPage::OnInitDialog();
  94.     
  95.     Doc = (CGnucleusDoc *) ((CGnucleusApp *) AfxGetApp())->MasterDoc;
  96.  
  97.     CRect rect;
  98.  
  99.     // speed up apply with a lot of files shared if nothing was changed.
  100.     m_bFilesSharedModified = false;
  101.  
  102.     // Setup shared list
  103.     m_lstShared.GetWindowRect(&rect);
  104.  
  105.     m_lstShared.InsertColumn(0, "Directory", LVCFMT_LEFT,
  106.         (rect.Width() - 30) * 2/3, 0);
  107.     m_lstShared.InsertColumn(1, "File Count", LVCFMT_LEFT,
  108.         (rect.Width() - 30) * 1/3, 1);
  109.  
  110.     m_lstShared.SetExtendedStyle(LVS_EX_FULLROWSELECT);
  111.     
  112.     // Maximum simultaneous uploads
  113.     if(Doc->m_MaxUploads)
  114.     {
  115.         m_chkMaxUL.SetCheck(1);
  116.         m_ebMaxUL.SetWindowText( DWrdtoStr(Doc->m_MaxUploads));
  117.     }
  118.     else
  119.     {
  120.         m_ebMaxUL.SetWindowText("Unlim.");
  121.         m_ebMaxUL.EnableWindow(FALSE);
  122.     }
  123.  
  124.     // Auto clear uploads
  125.     if(Doc->m_AutoClearUL)
  126.         m_chkClearUL.SetCheck(1);
  127.  
  128.     // Load up the listbox
  129.     std::vector<CString>::iterator itDir;
  130.     std::vector<DWORD>::iterator itCount;
  131.  
  132.     for (itDir = Doc->SharedDirList.begin(), itCount = Doc->SharedDirCount.begin(); 
  133.          itDir != Doc->SharedDirList.end(), itCount != Doc->SharedDirCount.end(); 
  134.          itDir++, itCount++)
  135.     {
  136.         int row = m_lstShared.GetItemCount();
  137.         
  138.         m_lstShared.InsertItem(row, (*itDir));
  139.         m_lstShared.SetItemText(row, 1, DWrdtoStr((*itCount)));
  140.     }
  141.  
  142.     // Set the recuse check as default
  143.     m_chkRecurse.SetCheck(1);
  144.  
  145.     return TRUE;  // return TRUE unless you set the focus to a control
  146.                   // EXCEPTION: OCX Property Pages should return FALSE
  147. }
  148.  
  149. void CPrefUpload::OnCheckMaxUl() 
  150. {
  151.     if(m_chkMaxUL.GetCheck())
  152.     {
  153.         m_ebMaxUL.SetWindowText( DWrdtoStr(Doc->m_MaxUploads) );
  154.         m_ebMaxUL.EnableWindow();
  155.     }
  156.     else
  157.     {
  158.         m_ebMaxUL.SetWindowText("Unlim.");
  159.         m_ebMaxUL.EnableWindow(FALSE);
  160.     }
  161.  
  162.     SetModified();
  163. }
  164.  
  165. void CPrefUpload::OnChangeEditMaxUl() 
  166. {
  167.     SetModified();
  168. }
  169.  
  170. void CPrefUpload::OnCheckClearUl() 
  171. {
  172.     SetModified();
  173. }
  174.  
  175. void CPrefUpload::OnButtonAdd() 
  176. {
  177.     CFileFind finder;
  178.     CString File;
  179.  
  180.  
  181.     // get updated string
  182.      m_ebEditShared.GetWindowText(File);
  183.  
  184.      if(File.IsEmpty() )
  185.      {
  186.          // call browse routine
  187.          OnButtonBrowseShare();
  188.          m_ebEditShared.GetWindowText(File);
  189.      }
  190.     
  191.     if(!File.IsEmpty() )
  192.     {
  193.         // Simple check to see if the directory actually exists.  Will not catch entering a file name.
  194.         SHFILEINFO psfi;
  195.         SHGetFileInfo(File, NULL, &psfi, sizeof(SHFILEINFO), SHGFI_DISPLAYNAME);
  196.         if(strlen(psfi.szDisplayName) )
  197.         {
  198.             // clear edit box,
  199.             m_ebEditShared.Clear();
  200.             
  201.             DWORD FileCount = Recurse(File);
  202.  
  203.             if(m_chkRecurse.GetCheck())
  204.                 File += ", Recursive";
  205.  
  206.             int row = m_lstShared.GetItemCount();
  207.  
  208.             // Add the directory to the list box
  209.             m_lstShared.InsertItem(row, File);
  210.  
  211.             // Get the number of files inside the directory
  212.             m_lstShared.SetItemText(row, 1, DWrdtoStr(FileCount));
  213.  
  214.             // clear out of edit box.
  215.             m_ebEditShared.SetWindowText("");
  216.  
  217.             SetModified();
  218.  
  219.             m_bFilesSharedModified = true;
  220.         }
  221.         else
  222.         {
  223.             MessageBox("Please Enter a valid directory!", "Invalid Directory", MB_ICONEXCLAMATION | MB_OK);
  224.         }
  225.     }
  226. }
  227.  
  228. DWORD CPrefUpload::Recurse(LPCTSTR pstr)
  229. {
  230.     CFileFind finder;
  231.  
  232.     DWORD FileCount = 0;
  233.  
  234.     // build a string with wildcards
  235.     CString strWildcard(pstr);
  236.  
  237.     if(strWildcard.GetAt( strWildcard.GetLength() - 1) != '\\')
  238.         strWildcard += _T("\\*.*");
  239.     else
  240.         strWildcard += _T("*.*");
  241.  
  242.     // start working for files
  243.     BOOL bWorking = finder.FindFile(strWildcard);
  244.  
  245.     while (bWorking)
  246.     {
  247.         bWorking = finder.FindNextFile();
  248.  
  249.         // skip . and .. files
  250.         if (finder.IsDots())
  251.          continue;
  252.  
  253.         // if it's a directory, recursively search it
  254.         if (finder.IsDirectory())
  255.         {
  256.         CString str = finder.GetFilePath();
  257.  
  258.             if(m_chkRecurse.GetCheck())
  259.                 FileCount += Recurse(str);
  260.         }
  261.         else
  262.             FileCount++;
  263.     }
  264.  
  265.     finder.Close();
  266.  
  267.     return FileCount;
  268. }
  269.  
  270.  
  271. void CPrefUpload::OnButtonRemove() 
  272. {
  273.     if(m_lstShared.GetSelectionMark() != -1)
  274.         m_lstShared.DeleteItem( m_lstShared.GetSelectionMark() );
  275.  
  276.     m_bFilesSharedModified = true;
  277.     
  278.     SetModified();
  279. }
  280.  
  281. void CPrefUpload::OnChangeEditSharedir() 
  282. {
  283.     SetModified();
  284. }
  285.  
  286. //: Browses for new a directory to share, then deposits it into the text box.
  287. void CPrefUpload::OnButtonBrowseShare() 
  288. {
  289.     // Get the directory to add from the user
  290.     char *Directory = new char[255];
  291.  
  292.     LPBROWSEINFO Settings = new BROWSEINFO;
  293.     Settings->hwndOwner = m_hWnd;
  294.     Settings->pidlRoot  = NULL;
  295.     Settings->pszDisplayName = Directory;
  296.     Settings->lpszTitle = "Choose a directory to share...";
  297.     Settings->ulFlags   = BIF_RETURNONLYFSDIRS;
  298.     Settings->lpfn      = NULL;
  299.     Settings->lParam    = NULL;
  300.     Settings->iImage    = NULL;
  301.  
  302.     LPCITEMIDLIST FolderID = SHBrowseForFolder(Settings);
  303.     delete Settings;
  304.  
  305.     if(FolderID)
  306.     {
  307.         SHGetPathFromIDList(FolderID, Directory);
  308.         m_ebEditShared.SetWindowText(Directory);
  309.     }
  310.  
  311.     delete [] Directory;
  312. }
  313.  
  314.  
  315. BOOL CPrefUpload::OnApply() 
  316. {
  317.     CString store;
  318.  
  319.     // Maximum simultaneous uploads
  320.     if(m_chkMaxUL.GetCheck())
  321.     {
  322.         m_ebMaxUL.GetWindowText(store);
  323.  
  324.         if(store != "")
  325.             Doc->m_MaxUploads = atoi(store);
  326.     }
  327.     else
  328.         Doc->m_MaxUploads = 0;
  329.  
  330.     // Auto clear uploads
  331.     if(m_chkClearUL.GetCheck())
  332.         Doc->m_AutoClearUL = 1;
  333.     else
  334.         Doc->m_AutoClearUL = 0;
  335.  
  336.     // Rebuild the list if modified
  337.     if(m_bFilesSharedModified)
  338.     {
  339.         Doc->LockUploadData (true);
  340.         
  341.         Doc->SharedDirList.clear();
  342.         Doc->SharedDirCount.clear();
  343.         Doc->SharedDirSize.clear();
  344.  
  345.         Doc->SharedFiles.clear();
  346.         Doc->SharedSizes.clear();
  347.  
  348.         for(int row = 0; row < m_lstShared.GetItemCount(); row++)
  349.         {
  350.             Doc->SharedDirList.push_back( m_lstShared.GetItemText(row, 0) );
  351.             Doc->SharedDirCount.push_back(0);
  352.             Doc->SharedDirSize.push_back(0);
  353.         }
  354.  
  355.         Doc->UnlockUploadData (true);
  356.  
  357.         Doc->LoadShare();
  358.  
  359.         m_bFilesSharedModified = false;
  360.     }
  361.  
  362.     return CPropertyPage::OnApply();
  363. }
  364.  
  365. void CPrefUpload::OnClickListShared(NMHDR* pNMHDR, LRESULT* pResult) 
  366. {
  367.     *pResult = 0;
  368.     int i_item = 0;
  369.  
  370.     if( (i_item = m_lstShared.GetSelectionMark()) != -1 )
  371.     {
  372.         CString item_text = m_lstShared.GetItemText(i_item, 0);
  373.  
  374.         if( item_text.Find(", Recursive") != -1)
  375.         {
  376.             item_text = item_text.Left(item_text.GetLength() - 11);
  377.         }
  378.  
  379.         m_ebEditShared.SetWindowText(item_text);
  380.     }
  381.     
  382. }
  383.